package outerapi

import (
	"YingShop/log"
	outerapiresponse "YingShop/outerApi/outerApiResponse"
	"YingShop/utils/request"
	"encoding/json"
	"fmt"
)

const WxAppHost = "https://api.weixin.qq.com"

type WxAppApi struct {
	Appid  string
	Secret string
}

func (WxApiObj *WxAppApi) Code2Session(jsCode string) outerapiresponse.Code2SessionRes {
	queryParams := make(map[string]string)
	queryParams["appid"] = WxApiObj.Appid
	queryParams["secret"] = WxApiObj.Secret
	queryParams["js_code"] = jsCode
	queryParams["grant_type"] = "authorization_code"
	res, err := request.Get(WxAppHost+"/sns/jscode2session", make(map[string]string, 0), queryParams)
	if err != nil {
		log.Error(fmt.Sprintf("请求wx登录接口出错,%s", err))
	}
	wxRes := outerapiresponse.Code2SessionRes{}
	json.Unmarshal(res, &wxRes)
	return wxRes
}
